package com.hx.vending.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.vending.bean.Apk;
import com.hx.vending.dao.ApkMapper;
import com.hx.vending.service.IApkService;
import com.hx.vending.util.AliyunOSSClientUtil;
import com.hx.vending.util.DateUtil;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/4/8.
 */
@Service
public class ApkService implements IApkService{


    @Resource
    private ApkMapper apkMapper;

    /**
     * 保存apk信息
     * @param apk
     * @return
     */
    @Override
    public Msg insertApk(Apk apk) {

        if (ToolUtil.isEmpty(apk.getApkUrl())) {
            return Msg.fail().add("msg", "未获取apk文件地址");
        }

        if (ToolUtil.isEmpty(apk.getApkType())) {
            return Msg.fail().add("msg", "未输入apk类别");
        }

//        Apk checkApk = new Apk();
//        checkApk.setApkName(apk.getApkName());
//        Apk selectApk = this.apkMapper.selectByPrimaryKeySelective(checkApk);
//
//        if (ToolUtil.isNotEmpty(selectApk)) {
//            return Msg.fail().add("msg", "名称重复");
//        }

        apk.setApkTime(DateUtil.getTime());
        int i = this.apkMapper.insertSelective(apk);

        return Msg.success();
    }



    /**
     * 删除apk信息
     * @param apkIds    1-2-3  id
     * @return
     */
    @Override
    public Msg deleteApk(String apkIds) {

        if (ToolUtil.isEmpty(apkIds)) {
            return Msg.fail().add("msg", "未选择要删除的apk");
        }

        String[] apkId = apkIds.split("-");

        for (String id: apkId) {
            //查询设备信息
            Apk apk = new Apk();
            apk.setApkId(Integer.parseInt(id));
            Apk selectApk = this.apkMapper.selectByPrimaryKeySelective(apk);
            //不存在删除下一个
            if (ToolUtil.isEmpty(selectApk)) {
                continue;
            }
            //删除信息并删除文件
            int i = this.apkMapper.deleteByPrimaryKey(Integer.parseInt(id));
            if (i != 0) {
                AliyunOSSClientUtil.deleteObject(selectApk.getApkUrl());
            }

        }

        return Msg.success();
    }



    /**
     * 修改apk信息
     * @param apk
     * @return
     */
    @Override
    public Msg updateApk(Apk apk) {

        if (ToolUtil.isEmpty(apk.getApkId())) {
            return Msg.fail().add("msg", "请选择要修改的apk");
        }

        Apk selectApk = new Apk();
        selectApk.setApkId(apk.getApkId());
        Apk resultApk = this.apkMapper.selectByPrimaryKeySelective(selectApk);
        if (ToolUtil.isEmpty(resultApk)) {
            return Msg.fail().add("msg","当前apk不存在");
        }

        int  i = this.apkMapper.updateByPrimaryKeySelective(apk);

        if (i != 0) {
            if (ToolUtil.isNotEmpty(apk.getApkUrl())) {
                //如果是新传入的文件地址不一样，就把原来的文件删除
                if (ToolUtil.isNotEmpty(resultApk.getApkUrl()) && !resultApk.getApkUrl().equals(apk.getApkUrl())) {
                    AliyunOSSClientUtil.deleteObject(resultApk.getApkUrl());
                }
            }
            return Msg.success();
        }

        return Msg.fail();
    }




    /**
     * 查询所有apk文件信息
     * @param apk
     * @return
     */
    @Override
    public Msg selectAllApkMessage(Apk apk, Integer pageSize, Integer pageNum) {

        if (ToolUtil.isEmpty(apk)) {
            return Msg.fail().add("msg", "请输入查询条件");
        }

        if (apk.getApkVersionName() == "") {
            apk.setApkVersionName(null);
        }
        if (apk.getApkStartTime() == "") {
            apk.setApkStartTime(null);
        }
        if (apk.getApkEndTime() == "") {
            apk.setApkEndTime(null);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Apk> selectApks = this.apkMapper.selectByExampleSelective(apk);
        PageInfo page = new PageInfo(selectApks, 5);

        return Msg.success().add("apks", selectApks).add("pageNum", pageNum).add("pageSize", pageSize).add("pages", page.getPages()).add("pageTotal", page.getTotal());
    }
}
